﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using UcAsp.Models;
namespace UcAsp.WorkFrame
{
    public class ArticleSubjects
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        public static List<Models.ArticleSubjects> GetListArticleSubjects
        {
            get
            {
                List<Models.ArticleSubjects> list = new List<Models.ArticleSubjects>();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path + "\\Content\\config\\ArticleSubjects.xml");
                string xmlpath = ("root/AdvPositions");
                if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                {
                    XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                    foreach (XmlNode xns in xmlList)
                    {

                        Models.ArticleSubjects model = new Models.ArticleSubjects();

                        foreach (XmlNode xn in xns)
                        {

                            Parser.XmlToObject(model, xn.Name, xn.InnerText);
                        }
                        list.Add(model);
                    }
                }
                return list;
            }
        }
        public static Models.ArticleSubjects GetArticleSubjects(int id)
        {
            Models.ArticleSubjects adv = GetListArticleSubjects.FirstOrDefault(o => o.SubjectId == id);
            return adv;
        }

        public static bool SaveArticleSubjects(Models.ArticleSubjects model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\Content\\config\\ArticleSubjects.xml");
            string xmlpath = ("root/AdvPositions");
            XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);

            foreach (XmlNode xns in xmlList)
            {


                if (xns["SubjectId"].InnerText == model.SubjectId.ToString())
                {
                    xns["SubjectName"].InnerText = model.SubjectName;
                    xns["Categories"].InnerText = model.Categories.ToString();
                    xns["CategoryName"].InnerText = model.CategoryName;
                    xns["Keywords"].InnerText = model.Keywords;
                    xns["MaxNum"].InnerText = model.MaxNum.ToString();
                    xns["Themes"].InnerText = model.Themes;
                }
            }
            xmlDoc.Save(path + "\\Content\\config\\ArticleSubjects.xml");
            return true;
        }
        public static List<Categories> GetMenu()
        {
              UcAspNetEntities entities = new UcAspNetEntities();
            List<Categories> list = new List<Categories>();
            list = entities.Categoriess.Where(o => o.ParentID == 11).OrderByDescending(o => o.Rate).ToList();
            return list;
        }

    }
}
